package scct.yangxianpeng.dao;

import scct.yangxianpeng.model.Brand;
import scct.yangxianpeng.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BrandDao {
    //    获取品牌列表
    public List<Brand> getAllbrands() {
        List<Brand> brands = new ArrayList<>();
        Connection conn = DBUtils.getConnection();
        String sql = "select * from sm_brand";
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String company = rs.getString("company");
                brands.add(new Brand(id, name, company));
            }
            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return brands;
    }
}
